import request from '@/utils/request';
import CryptoJs from 'crypto-js'

export const tempDate = new Date().valueOf().toString().substring(0,6);

export async function query() {
  return request('/api/users');
}

export async function queryCurrent(params) {
  return request('/hr-ssc/api/api-admin/getUserInfo', {
    showToast: false,
  });
}

export async function queryNotices() {
  return request('/api/notices');
}

export function encryptPassword(password) {
  let key = CryptoJs.enc.Utf8.parse('N!KsTS3R^rwY&bQ7@c*O' + '&&' + new Date().valueOf().toString().substring(0,6) + '0000');
  let encrypted = CryptoJs.AES.encrypt(CryptoJs.enc.Utf8.parse(password), key, {mode:CryptoJs.mode.ECB,padding: CryptoJs.pad.Pkcs7});
  return encrypted.toString();
}

export async function login(params) {
  let url;
  if(params.username){
    url = `loginByUserAndPwd`;
    return request(`/hr-ssc/api/api-admin/` + url, {
      method: 'post',
      data:{
        username:params.username,
        password:encryptPassword(params.password)
      }
    }).catch(() => {});
  }else{
    url = `login?code=${encodeURIComponent(decodeURIComponent(params))}`;
    return request(`/hr-ssc/api/api-admin/` + url, {
      method: 'get',
    }).catch(() => {});
  }
}

export async function getFakeCaptcha(mobile) {
  return request(`/api/login/captcha?mobile=${mobile}`);
}

export async function logout() {
  return request('/hr-ssc/api/api-admin/exit');
}

export async function editPswd(params) {
  return request('/hr-ssc/api/api-admin/roleAssignment/updateUserPwdByUsername', {
    method: 'POST',
    data: {
      ...params
    },
    requestType: 'form'
  }).then((res) => {
    return res;
  });
}


